<template>
  <div id="login_container">
    <div class="login_box">
      <!-- 图片区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="民主 自由" />
      </div>
      <!-- 表单验证区域 -->
      <el-form label-width="0px" class="login_form" :model="loginForm" :rules="rules" ref="loginFormRef">
        <!-- 用户名验证区 -->
        <el-form-item class="login_form_item" prop="username">
          <el-input placeholder="请输入密码" prefix-icon="el-icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码验证区 -->
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" class="btn" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 1.这是登录表单的数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 2.表单验证规则
      rules: {
        username: [
          { required: true, message: '账号不能为空', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 3, max: 16, message: '请输入正确的密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 1.点击重置
    resetForm () {
      this.$refs.loginFormRef.resetFields()
    },
    // 2.提交表单之前进行预验证
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return
        }
        // data是请求结果中获取到的属性, 解构出来的可以命名为res
        const { data: res } = await this.$http.post('/login', this.loginForm)
        if (res.meta.status === 200) {
          this.$message.success('登录成功')
          // 1.将登录成功后的token, 保存到客户端的sessionStorage中
          // 2.项目中除了登录之外的其他api接口, 必须在登录之后才能访问
          // 3.token只应在当前网站打开期间生效,所以将token保存在sessionStorage中
          window.sessionStorage.setItem('token', res.data.token)
          // 4.通过编程式导航跳转到后台主页, 路由地址是/home
          this.$router.push('/home')
        } else if (res.meta.msg === '密码错误') {
          this.$message.error('密码错误')
        } else {
          this.$message.error('账号不存在')
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
#login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  background-color: #fff;
  width: 45rem;
  height: 30rem;
  border-radius: 0.5rem;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .login_form {
    position: absolute;
    bottom: 0;
    width: 25rem;
    left: 22.5rem;
    margin-left: -12.5rem;
    .el-input {
      font-size: 1.2rem;
    }
  }
  .btns {
    display: flex;
    justify-content: flex-end;
    padding: 2rem;
  }
  .avatar_box {
    width: 13rem;
    height: 13rem;
    border: 0.1rem solid #eee;
    border-radius: 50%;
    padding: 1rem;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
      border: 0.1rem solid #eee;
    }
  }
}
</style>
